package org.example.salesystem.controller;

import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.ShopManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/ShopMessage")
@CrossOrigin(origins="*")
public class ShopManController {
    @Autowired
    private ShopManService shopManService;

    @RequestMapping("/UpdateShop")
    public ResultBody updateShop(@RequestBody Map<String, Object> map) {
        if (map.get("code") == null) {
            return new ResultBody(404, "缺少参数", false);
        }

        String code = (String) map.get("code");
        String name = (String) map.get("name");
        String description = (String) map.get("description");
        String category = (String) map.get("category");
        String phone = (String) map.get("phone");
        String avatar_Url = (String) map.get("avatar_Url");
        Object object = map.get("price");
        double price;
        if (object instanceof Double) {
            price = (double) object;
        }
        else if (object instanceof Integer) {
            price = ((Integer) object).doubleValue();
        }
        else {
            return shopManService.updateShop1(code,name,description,category,phone,avatar_Url);
        }
        //进行传值
        return shopManService.updateShop2(code,name,description,price,category,phone,avatar_Url);
    }

    @RequestMapping("/deleteShop")
    public ResultBody deleteShop(@RequestBody Map<String ,Object> map) {
        if(map.get("code")==null) {
            return new ResultBody(400,"缺少参数",false);
        }
        String code = (String) map.get("code");
        if(shopManService.DeMessage(code)==1)
        {
            return  new ResultBody(200,"下架成功",true);
        }
        else
            return new ResultBody(400,"下架失败",false);
    }

}
